package com.yinhai.message.activemq.monitor;

import java.util.ArrayList;
import java.util.List;

import javax.management.MBeanServerConnection;
import javax.management.MBeanServerInvocationHandler;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.activemq.broker.jmx.BrokerViewMBean;
import org.apache.activemq.broker.jmx.QueueViewMBean;

import com.yinhai.message.manage.monitor.vo.ConsumerVo;
import com.yinhai.message.manage.monitor.vo.MqQueueVo;
import com.yinhai.message.manage.monitor.vo.ProducerVo;

/**
 * @author yanglq 2017.3.23
 */
public class StateMonitor {

    private static final String connectorPort = "1099";
    private static final String connectorPath = "/jmxrmi";
    private static final String jmxDomain = "org.apache.activemq";// 必须与activemq.xml中的jmxDomainName一致
    private static final String brokerName = "localhost";// 必须与activemq.xml中的brokerName一致
    private static final String QUEUE_NAME = "com.yinhai.message.api.activemq.vo.AMQMessageVo";

    public List<MqQueueVo> getQueueInfo() throws Exception{
		List<MqQueueVo> list = new ArrayList<>();
		MqQueueVo vo = null;
		JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + connectorPort+ connectorPath);
		JMXConnector connector = JMXConnectorFactory.connect(url);
		connector.connect();
		MBeanServerConnection connection = connector.getMBeanServerConnection();
		ObjectName name = new ObjectName(jmxDomain + ":brokerName="+ brokerName + ",type=Broker");
		BrokerViewMBean mBean = MBeanServerInvocationHandler.newProxyInstance(connection, name, BrokerViewMBean.class, true);
		for (ObjectName queueName : mBean.getQueues()) {
			if(queueName.toString().equals("QUEUE_NAME")){
				vo = new MqQueueVo();
				QueueViewMBean queueMBean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
				vo.setQueueName(queueMBean.getName());
				vo.setQueueSize(queueMBean.getQueueSize());
				vo.setDeQueue(queueMBean.getDequeueCount());
				vo.setEnQueue(queueMBean.getEnqueueCount());
				list.add(vo);
			}
		}
		connector.close();
    	return list;
    }
    public List<ConsumerVo> getConsumerInfo()throws Exception{
    	List<ConsumerVo> list = new ArrayList<>();
    	ConsumerVo vo = null;
    	JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + connectorPort+ connectorPath);
		JMXConnector connector = JMXConnectorFactory.connect(url);
		connector.connect();
		MBeanServerConnection connection = connector.getMBeanServerConnection();
		ObjectName name = new ObjectName(jmxDomain + ":brokerName="+ brokerName + ",type=Broker");
		BrokerViewMBean mBean = MBeanServerInvocationHandler.newProxyInstance(connection, name, BrokerViewMBean.class, true);
		for (ObjectName queueName : mBean.getQueues()) {
			if(queueName.toString().equals("QUEUE_NAME")){
				vo = new ConsumerVo();
				QueueViewMBean queueMBean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
				vo.setCount(queueMBean.getConsumerCount());
				list.add(vo);
			}
		}
		connector.close();
    	return list;
    }
    public List<ProducerVo> getProducerInfo()throws Exception{
    	List<ProducerVo> list = new ArrayList<>();
    	ProducerVo vo = null;
    	JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:" + connectorPort+ connectorPath);
		JMXConnector connector = JMXConnectorFactory.connect(url);
		connector.connect();
		MBeanServerConnection connection = connector.getMBeanServerConnection();
		ObjectName name = new ObjectName(jmxDomain + ":brokerName="+ brokerName + ",type=Broker");
		BrokerViewMBean mBean = MBeanServerInvocationHandler.newProxyInstance(connection, name, BrokerViewMBean.class, true);
		for (ObjectName queueName : mBean.getQueues()) {
			if(queueName.toString().equals("QUEUE_NAME")){
				vo = new ProducerVo();
				QueueViewMBean queueMBean = MBeanServerInvocationHandler.newProxyInstance(connection, queueName, QueueViewMBean.class, true);
				vo.setSendCount(queueMBean.getProducerCount());
				list.add(vo);
			}
		}
		connector.close();
    	return list;
    }
}
